// pages/publish/detail/details/details.js
import { request } from '../../../../request/index.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    sciences: [
      {
        id: 1,
        title: '狗为什么不能吃巧克力',
        variety: '狗',
        pic: 'https://img.yzcdn.cn/vant/cat.jpeg',
      },
    ], 
    pageNum: 1,
    pageSize: 10,
    searchValue: '',
  },
  onLoad(){
    this.loadSciences();
  },
  loadSciences() {
    const token = wx.getStorageSync('token');
    const that = this;
    let data = {
      pageNum: that.data.pageNum,
      pageSize: that.data.pageSize,
    }
    if(that.data.searchValue){
      data.petVariety = that.data.searchValue 
    }
    request({
      url: '/petScience/listPage',
      method: 'GET',
      data: data,
      header: {
        'author': token ? token : '',
        'content-type': 'application/x-www-form-urlencoded' // 设置请求头为表单数据格式
      }
    }).then(res => {
      if (res.code === 0){
        const newData = res.data.items;
        if (that.data.pageNum === 1) {
          that.setData({ sciences: newData }); // 下拉刷新，覆盖数据
        } else {
          that.setData({ sciences: that.data.sciences.concat(newData) }); // 上拉加载，追加数据
        }
        that.setData({ pageNum: that.data.pageNum + 1 }); // 更新页码
      } else {
        wx.showToast({
          title: '错误',
          icon: 'error'
        })
      }
    })
  }
})